package patrol.mobile.com.mobilepatrol.persenter;

import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.api.ByteCallback;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.utils.Compress;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;
import patrol.mobile.com.mobilepatrol.view.DataSumView;

/**
 * Created by apple on 17/9/27.
 */

public class DataSumPer {
    private DataSumView mView;
    String type;
    public DataSumPer(DataSumView mView, String type) {
        this.mView = mView;
        this.type = type;
    }
    public void getDataCount() {
        OkHttpUtils
                .post()
                .addParams("type",type)
                .url(SPUtils.get(Constants.current_ser,"") + Urls.initData)
                .build()
                .execute(new ByteCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(byte[] response, int id) {
                        try {
                            String result = Compress.decompressByteArray(response);
                            JSONObject object = new JSONObject(result);
                        if (object.getInt("code")==200){
                            mView.hideLoading();
                            JSONObject value = object.getJSONObject("value");
                            int count = value.optInt("surveyDatacount");
                            mView.onDataSumSuccess(count);
                        }else{
                            mView.onError("初始化车辆信息失败");
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }
}
